<template>
  <el-upload
    :data="data"
    :id="id"
    :action="uploadUri"
    :show-file-list="false"
    :before-upload="beforeAvatarUpload"
    :on-success="handleAvatarSuccess">
    <slot></slot>
  </el-upload>
</template>

<script>
import _g from '../assets/utils/global'

export default {
  props: ['id', 'data'],
  data() {
    return { uploadUri:PATH+"/company/getPicAddress.do"}
  },
  mounted() {
    console.log('component url',this.url)
    if (this.url) {
      this.action = PATH + this.url
    } else {
      this.action = PATH + 'index/upload'
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      if (res.status == 0) {
        this.$emit('input', res.data)
      } else {
        _g.message('上传失败', 'warning')
      }
    },
    beforeAvatarUpload(file) {

      const isJpeg = file.type === 'image/jpeg'
      const isPng = file.type === 'image/png'
      const isJpg = file.type === 'image/jpg'
      const isLt10M = file.size / 1024  /1024< 10

      if (!(isJpeg || isPng || isJpg)) {
        _g.message('上传头像图片只能是jpg、jpeg、png格式!', 'warning')
        return false
      }
      if (!isLt10M) {
        _g.message('上传头像图片大小不能超过10MB!', 'warning')
        return false
      }
      return (isJpeg || isPng || isJpg )&& isLt10M
    }
  },
  components: {}
}
</script>

<style lang="scss" scoped>
</style>
